package com.weilx959.spt.plugin.e2sp.controller;

import com.weilx959.spt.core.component.ComboBoxFormItemControl;
import com.weilx959.spt.core.component.TextAreaFormItemControl;
import com.weilx959.spt.core.config.FormControlConfig;
import com.weilx959.spt.plugin.e2sp.component.DialectListCell;
import com.weilx959.spt.plugin.e2sp.constant.Dialect;
import com.weilx959.spt.plugin.e2sp.constant.ExcelToSqlConstants;
import com.weilx959.spt.plugin.e2sp.context.ConfigurationContext;
import com.weilx959.spt.plugin.e2sp.model.Configuration;
import javafx.fxml.FXML;
import lombok.Getter;

import java.util.Optional;

public class ExcelToSqlConfigController {

    @FXML
    @Getter
    private FormControlConfig config;

    @FXML
    private ComboBoxFormItemControl<Dialect> dialectItem;

    @FXML
    private TextAreaFormItemControl checkTemplateItem;

    @FXML
    private void initialize() {
        dialectItem.getValueControl().setCellFactory(listView -> new DialectListCell());
        dialectItem.getValueControl().setButtonCell(new DialectListCell());
        dialectItem.setValue(Optional.ofNullable(ConfigurationContext.get())
            .map(Configuration::getDialect)
            .orElse(ExcelToSqlConstants.DEFAULT_DIALECT));

        checkTemplateItem.setValue(Optional.ofNullable(ConfigurationContext.get())
            .map(Configuration::getCheckTemplate)
            .orElse(ExcelToSqlConstants.DEFAULT_CHECK_SQL_TEMPLATE));
        config.submit();
    }
}
